namespace CISLib
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;

    public class ManageRole
    {
        public static SqlDataReader GetRole()
        {
            string strSQL = "Select * from TB_Role order by RoleName ";
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            return MyDB.DBReader;
        }

        public static SqlDataReader GetRole(Guid areaId)
        {
            string strSQL = "Select * from TB_Role where AreaId=@AreaId order by RoleName ";
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(areaId, "AreaId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            return MyDB.DBReader;
        }

        public static Role[] GetRoleAry()
        {
            SqlDataReader MyReader = null;
            ArrayList roleList = new ArrayList();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "Select * from TB_Role order by RoleName";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            while (MyReader.Read())
            {
                Role role = new Role();
                role.RoleId = MyReader.GetGuid(0);
                role.RoleName = MyReader.GetString(1);
                role.RoleDescr = MyReader.GetString(2);
                role.TypeA = MyReader.GetInt32(3);
                role.TypeB = MyReader.GetInt32(4);
                role.TypeC = MyReader.GetInt32(5);
                role.TypeD = MyReader.GetInt32(6);
                role.IsUnmasterView = MyReader.GetInt32(8);
                roleList.Add(role);
            }
            MyDB.DBClose();
            return (Role[]) roleList.ToArray(typeof(Role));
        }

        public static Role[] GetRoleAry(Guid areaId)
        {
            SqlDataReader MyReader = null;
            ArrayList roleList = new ArrayList();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "Select * from TB_Role where AreaId=@AreaId  order by RoleName";
            MyDB.AddParameters(areaId, "AreaId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            while (MyReader.Read())
            {
                Role role = new Role();
                role.RoleId = MyReader.GetGuid(0);
                role.RoleName = MyReader.GetString(1);
                role.RoleDescr = MyReader.GetString(2);
                role.TypeA = MyReader.GetInt32(3);
                role.TypeB = MyReader.GetInt32(4);
                role.TypeC = MyReader.GetInt32(5);
                role.TypeD = MyReader.GetInt32(6);
                role.IsUnmasterView = MyReader.GetInt32(8);
                roleList.Add(role);
            }
            MyDB.DBClose();
            return (Role[]) roleList.ToArray(typeof(Role));
        }

        public static Role GetRoleViaId(Guid id)
        {
            SqlDataReader MyReader = null;
            Role role = new Role();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "select * from tb_Role where RoleId = @RoleId";
            MyDB.AddParameters(id, "RoleId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            if (MyReader.Read())
            {
                role.RoleId = MyReader.GetGuid(0);
                role.RoleName = MyReader.GetString(1);
                role.RoleDescr = MyReader.GetString(2);
                role.TypeA = MyReader.GetInt32(3);
                role.TypeB = MyReader.GetInt32(4);
                role.TypeC = MyReader.GetInt32(5);
                role.TypeD = MyReader.GetInt32(6);
                role.IsUnmasterView = MyReader.GetInt32(8);
                role.CheckAuthorization = MyReader.GetInt32(9);
            }
            MyDB.DBClose();
            return role;
        }

        public static void InsertNewRole(string roleName, string roleDescr, string strTypeA, string strTypeB, string strTypeC, string strTypeD, Guid areaId, int isUnmasterView, string strKPIType)
        {
            Guid id = Guid.NewGuid();
            DBAccess MyDB = new DBAccess();
            int iTypeA = Convert.ToInt32(strTypeA);
            int iTypeB = Convert.ToInt32(strTypeB);
            int iTypeC = Convert.ToInt32(strTypeC);
            int iTypeD = Convert.ToInt32(strTypeD);
            int iKPIType = Convert.ToInt32(strKPIType);
            MyDB.DBOpen();
            MyDB.AddParameters(roleName, "RoleName", SqlDbType.VarChar);
            MyDB.AddParameters(roleDescr, "RoleDescr", SqlDbType.VarChar);
            MyDB.AddParameters(iTypeA, "AAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeB, "BAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeC, "CAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeD, "DAuthorization", SqlDbType.Int);
            MyDB.AddParameters(id, "RoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(areaId, "AreaId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(isUnmasterView, "IsUnmasterView", SqlDbType.Int);
            MyDB.AddParameters(iKPIType, "CheckAuthorization", SqlDbType.Int);
            string strSQL = "Insert into TB_Role (RoleName,RoleDescr,RoleId,AAuthorization,BAuthorization,CAuthorization,DAuthorization,AreaId,IsUnmasterView,CheckAuthorization) values (@RoleName,@RoleDescr,@RoleId,@AAuthorization,@BAuthorization,@CAuthorization,@DAuthorization,@AreaId,@IsUnmasterView,@CheckAuthorization)";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static bool IsExistRoleInOtherTable(Guid roleId)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(roleId, "RoleId", SqlDbType.UniqueIdentifier);
            string strSQL = "select * from TB_SysUser where RoleId = @RoleId";
            MyDB.DBSetSelect(strSQL);
            if (MyDB.DBExecuteScalar() == null)
            {
                MyDB.DBClose();
                return false;
            }
            MyDB.DBClose();
            return true;
        }

        public static void RemoveRoleViaId(Guid id)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "RoleId", SqlDbType.UniqueIdentifier);
            string strSQL = "Delete From TB_Role where RoleId = @RoleId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateRole(Guid roleId, string roleName, string roleDescr, string strTypeA, string strTypeB, string strTypeC, string strTypeD, int isUnmasterView, string strKPIType)
        {
            DBAccess MyDB = new DBAccess();
            int iTypeA = Convert.ToInt32(strTypeA);
            int iTypeB = Convert.ToInt32(strTypeB);
            int iTypeC = Convert.ToInt32(strTypeC);
            int iTypeD = Convert.ToInt32(strTypeD);
            int iKPIType = Convert.ToInt32(strKPIType);
            MyDB.DBOpen();
            MyDB.AddParameters(roleName, "RoleName", SqlDbType.VarChar);
            MyDB.AddParameters(roleDescr, "RoleDescr", SqlDbType.VarChar);
            MyDB.AddParameters(iTypeA, "AAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeB, "BAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeC, "CAuthorization", SqlDbType.Int);
            MyDB.AddParameters(iTypeD, "DAuthorization", SqlDbType.Int);
            MyDB.AddParameters(roleId, "RoleId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(isUnmasterView, "IsUnmasterView", SqlDbType.Int);
            MyDB.AddParameters(iKPIType, "CheckAuthorization", SqlDbType.Int);
            string strSQL = "Update TB_Role set RoleName = @RoleName,RoleDescr = @RoleDescr,AAuthorization = @AAuthorization,BAuthorization = @BAuthorization,CAuthorization = @CAuthorization,DAuthorization = @DAuthorization,IsUnmasterView=@IsUnmasterView,CheckAuthorization=@CheckAuthorization where RoleId = @RoleId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }
    }
}

